    #include "chat.h"
#include "ui_chat.h"
#include<QFont>
Chat::Chat(QTcpSocket *s,QWidget *parent) :
    socket(s),
    QDialog(parent),
    ui(new Ui::chat)
{
    widget = new QListWidget;
    ui->setupUi(this);
    ui->tabWidget->setTabText(0,"好友列表");
    ui->tabWidget->setTabText(1,"消息");
    connect(ui->listWidget_2,SIGNAL(itemDoubleClicked(QListWidgetItem *)),this,SLOT(ChatToSomebody(QListWidgetItem *)));
    cpack = new ChatPackage;
    adf = new AddFriend;
    areq = new AcceptRequest;

    //ui->tab_2->add




    bool a = connect(socket,SIGNAL(readyRead()),this,SLOT(DealreqAnswer()));
    if(a== true)
    {
        //QMessageBox::information(this,"ok","okkkkkkk");
    }else
    {
        //QMessageBox::information(this,"ok","不ok");
    }

    // connect(ui->label_p1,SIGNAL(clicked()),this,SLOT(clickEvent()));
    //connect(ui->listWidget);
    connect(ui->listWidget,SIGNAL(itemDoubleClicked(QListWidgetItem *)),this,SLOT(ChatToSomebody(QListWidgetItem *)));
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(AddFriendSlot()));
}

Chat::~Chat()
{
    if(chatframe)
        delete chatframe;
    delete ui;
}

void Chat::setInfo(QString name)
{
    //  QMessageBox::information(this,"fff",name);
    username = name;
    Request req;
    req.cmd = CMD_REQUEST_USERINFO;
    sprintf(req.username,username.toStdString().c_str());
    socket->write((const char *)&req,MSG_LENGTH);
}


void Chat::getFriendList()
{
    Request req;
    req.cmd = CMD_REQUEST_FRIENDSLIST;
    sprintf(req.username,username.toStdString().c_str());
    //QMessageBox::information(this,"eee",req.username);
    socket->write((const char *)&req,MSG_LENGTH);
}

void Chat::DealreqAnswer()
{

    QByteArray array = socket->read(1024);
    char * ptr = array.data();
    UserInfoMsg* user = (UserInfoMsg *)ptr;
    DataHeader* header = (DataHeader*)user;
   // QMessageBox::information(this,"ee",QString::number(header->cmd));

    if(header->cmd == CMD_GET_MSG)
    {
        qDebug()<<"ok";
        AnswerMsg * answer =(AnswerMsg*)ptr;
        qDebug()<<answer->username;
        qDebug()<<answer->pic;
        qDebug()<<answer->msgbody;
        msg_buffer = answer->msgbody;
        //cpack->from
        strcpy(cpack->from,answer->username);
        strcpy(cpack->msgbody,answer->msgbody);

        //msg_buffer = QByteArray::fromRawData(answer->msgbody,strlen(answer->msgbody));
        if(strcmp(answer->pic,":/new/prefix1/res/img/notice.png")==0)
        {
            flag_bak = 1;
        }
        item = new QListWidgetItem(QIcon(answer->pic), answer->username);

        ui->listWidget_2->addItem(item);
        return;

    }

    if(header->cmd == CMD_CHAT_TO_FRIEND)
    {
        static int a = 0;

            char * ptr = array.data();
            chatframe->setContent(ptr);


            return;

    }


    if(header->cmd == CMD_ANSWER_FRIENDLIST)
    {
        char pbuffer[128]={0};
        sprintf(pbuffer,":%s",user->path);

        ui->listWidget->addItem(new QListWidgetItem(QIcon(pbuffer), user->nickname));

        array = socket->read(1024);
        while(array.size()!=0)
        {


            char * ptr = array.data();
            UserInfoMsg* user = (UserInfoMsg *)ptr;
            char pbuffer[128]={0};
            sprintf(pbuffer,":%s",user->path);
            QListWidgetItem * item = new QListWidgetItem(QIcon(pbuffer), user->username);
            ui->listWidget->addItem(item);
            qDebug()<<pbuffer;
            array = socket->read(1024);

        }
        return;
    }
    if(header->cmd==CMD_REG_USERINFO)
    {
        char pbuffer[128]={0};
        sprintf(pbuffer,":%s",user->path);
        qDebug()<<pbuffer;
        ui->label_nickname->setText(user->nickname);
        ui->label_sig->setText(user->signature);

        QPixmap pic(pbuffer);
        QSize size(ui->label->width(),ui->label->height());
        pic = pic.scaled(size,Qt::KeepAspectRatio,Qt::SmoothTransformation);
        ui->label->setPixmap(pic);
        GetMsg getmsg;
        strcpy(getmsg.username,username.toStdString().c_str());
        socket->write((const char *)&getmsg,MSG_LENGTH);

    }



}

void Chat::clickEvent()
{
    QMessageBox::information(this,"test","test");
}

void Chat::ChatToSomebody(QListWidgetItem *item)
{


    qDebug()<<"text";
    qDebug()<<item->text();
    if(flag_bak == 1)
    {
        areq->SetAccount(username);
        areq->SetInfo(socket,item->text());
        areq->show();
        flag_bak = 0;
        return;
    }

    chatframe = new ChatFrame(socket,username,item->text());
    cpack->datalength = 12;


    chatframe->setContent((const char *)cpack);


    chatframe->show();

}

void Chat::AddFriendSlot()
{
    adf->SetSocket(socket,username);
    adf->show();
}
